﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Security;
using System.IO;
using CoyoEden.Models;
using System.Web;

namespace CoyoEden.Web.Controls
{
	/// <summary>
	/// View base,consumed by ViewManager
	/// </summary>
	public class ViewBase : UserControl
	{
		#region .ctor
		/// <summary>
		/// default.ctor
		/// </summary>
		public ViewBase()
		{
			ViewDatas = new Dictionary<string, object>();
		}
		#endregion

		#region properties
		/// <summary>
		/// View Data
		/// </summary>
		public Dictionary<string, object> ViewDatas { get; private set; }

		/// <summary>
		/// User Roles.Readonly
		/// </summary>
		public List<string> UserRoles
		{
			get
			{
				return Roles.GetAllRoles().ToList();
			}
		}
		public string UserName
		{
			get
			{
				return Page.User.Identity.Name;
			}
		}
		#endregion

		#region Methods
		/// <summary>
		/// Get View Path
		/// </summary>
		/// <param name="viewName"></param>
		/// <returns></returns>
		public string GetViewPath(string viewName)
		{
			string theme = PortalSettings.Instance.Theme;
			if (Request.QueryString["theme"] != null)
				theme = Request.QueryString["theme"];

			string path = string.Format("{0}themes/{1}/{2}.ascx", Utils.RelativeWebRoot, theme, viewName);//theme's ascx
			if (!File.Exists(Request.MapPath(path)))
			{
				path = string.Format("{0}Views/{1}.ascx", Utils.RelativeWebRoot, viewName);//default ascx
			}
			return path;
		}
		/// <summary>
		/// Get View Path
		/// </summary>
		/// <param name="request"></param>
		/// <param name="viewName"></param>
		/// <returns></returns>
		public static string GetViewPath(HttpRequest request, string viewName)
		{
			string theme = PortalSettings.Instance.Theme;
			if (request.QueryString["theme"] != null)
				theme = request.QueryString["theme"];

			string path = string.Format("{0}themes/{1}/{2}.ascx", Utils.RelativeWebRoot, theme, viewName);//theme's ascx
			if (!File.Exists(request.MapPath(path)))
			{
				path = string.Format("{0}Views/{1}.ascx", Utils.RelativeWebRoot, viewName);//default ascx
			}
			return path;
		}


		/// <summary>
		/// Render a topicview
		/// </summary>
		/// <param name="topic"></param>
		/// <returns></returns>
		public virtual string RenderTopicView(Topic topic, bool showAdminLink, bool tailContent)
		{
			var path = GetViewPath("TopicView");
			//Render the view
			using (StringWriter sw = new StringWriter())//TODO:Using ViewManager<T>
			{
				TopicViewBase control = (TopicViewBase)LoadControl(path);
				control.TheTopic = topic;
				control.ShowAdminLink = showAdminLink;
				control.TailContent = tailContent;
				control.RenderControl(new HtmlTextWriter(sw));
				var tempHtml = sw.ToString();
				return tempHtml;
			}
		}
		/// <summary>
		/// Render a vote view
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public virtual string RenderVoteView(Vote item) {
			var path = GetViewPath("VoteView");
			//Render the view
			using (StringWriter sw = new StringWriter())//TODO:Using ViewManager<T>
			{
				VoteViewBase control = (VoteViewBase)LoadControl(path);
				control.TheVote = item;
				control.RenderControl(new HtmlTextWriter(sw));
				var tempHtml = sw.ToString();
				return tempHtml;
			}
		}
		/// <summary>
		/// Render a topic comment view
		/// </summary>
		/// <param name="topic"></param>
		/// <returns></returns>
		public virtual string RenderCommentView(Comment item, Topic topic)
		{
			var path = GetViewPath("TopicCommentView");
			//Render the view
			using (StringWriter sw = new StringWriter())//TODO:Using ViewManager<T>
			{
				CommentViewBase control = (CommentViewBase)LoadControl(path);
				control.Comment = item;
				control.Topic = topic;
				control.RenderControl(new HtmlTextWriter(sw));
				var tempHtml = sw.ToString();
				return tempHtml;
			}
		}
		public virtual string RenderCategoryView(Category item)
		{
			var path = GetViewPath("CategoryView");
			using (StringWriter sw = new StringWriter())
			{
				//TODO:Using ViewManager<T>
				CategoryViewBase view = (CategoryViewBase)LoadControl(path);
				view.TheCategory = item;
				view.RenderControl(new HtmlTextWriter(sw));
				return sw.ToString();
			}
		}
		/// <summary>
		/// render a simple post view
		/// </summary>
		/// <param name="item"></param>
		/// <param name="showAdminLink"></param>
		/// <param name="showExcerpt"></param>
		/// <returns></returns>
		public virtual string RenderSimplePostView(Post item, bool showAdminLink, bool showExcerpt)
		{
			var path = GetViewPath("SimplePostView");
			//using (StringWriter sw = new StringWriter())
			//{
			//  //TODO:Using ViewManager<T>
			//  PostViewBase view = (PostViewBase)LoadControl(path);
			//  view.Post = item;
			//  view.ShowExcerpt = showExcerpt;
			//  view.ShowAdminLink = showAdminLink;
			//  view.RenderControl(new HtmlTextWriter(sw));
			//  return sw.ToString();
			//}
			ViewManager<PostViewBase> view = new ViewManager<PostViewBase>(path, Page);
			view.Control.Post = item;
			view.Control.ShowAdminLink = showAdminLink;
			view.Control.ShowExcerpt = showExcerpt;
			return view.Render();
		}
		/// <summary>
		/// Render a ratelist view
		/// </summary>
		/// <param name="relatedVote"></param>
		/// <returns></returns>
		public virtual string RenderRateListDetailView(Vote relatedVote) {
			var path = GetViewPath("RateListDetailView");
			ViewManager<VoteViewBase> view = new ViewManager<VoteViewBase>(path, Page);
			view.Control.VoteItemsShow =relatedVote.VoteItems;
			view.Control.TheVote = relatedVote;
			return view.Render();
		}
		#endregion
	}
}
